//
//  MBProgressHUD+WHCustomHUD.m
//  BinFengExpressOwner
//
//  Created by 邓伟浩 on 2018/10/22.
//  Copyright © 2018年 BinFeng. All rights reserved.
//

#import "MBProgressHUD+WHCustomHUD.h"
#import "UIImage+GIF.h"
#import "WHCustomImageView.h"

@implementation MBProgressHUD (WHCustomHUD)

+ (void)showHudInView:(UIView *)view {
    
    if (view == nil) view = [UIApplication sharedApplication].delegate.window;
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    
    //使用SDWebImage 放入gif 图片
    UIImage *image = [UIImage sd_animatedGIFNamed:@"hud"];
    WHCustomImageView *cusImageV = [[WHCustomImageView alloc] initWithImage:image];
    
    //    UIImageView *gifImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"hud"]];
    //    NSMutableArray *arrM = [[NSMutableArray alloc] init];
    //    for (int i = 0; i < 12; i ++) {
    //        UIImage *image = [UIImage imageNamed:@"hud"];
    //        [arrM addObject:image];
    //    }
    //    [gifImageView setAnimationImages:arrM];
    //    [gifImageView setAnimationDuration:1.5];
    //    [gifImageView setAnimationRepeatCount:0];
    //    [gifImageView startAnimating];
    
    // 再设置模式
    hud.mode = MBProgressHUDModeCustomView;
    
    hud.customView = cusImageV;
    
    //设置在hud影藏时将其从SuperView上移除,自定义情况下默认为NO
    hud.removeFromSuperViewOnHide = YES;
    
    //设置方框view为该模式后修改颜色才有效果
    
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    //设置方框view背景色
    hud.bezelView.backgroundColor = [UIColor clearColor];
    hud.label.text = @"正在加载";
    hud.label.textColor = CFontContentLightGrayColor;
    
    //设置总背景view的背景色，并带有透明效果
    //    hud.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
    //    [hud hideAnimated:YES afterDelay:1];
}

+ (void)hideHudWithMessage:(NSString *)message toView:(UIView *)view {
    [self hideHUDForView:view];
    [self showMessage:message toView:view];
}

+ (void)showMessage:(NSString *)text toView:(UIView *)view
{
    if (view == nil) view = [[UIApplication sharedApplication].windows lastObject];
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.label.text = text;
    // 再设置模式
    hud.mode = MBProgressHUDModeText;
    
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    //设置方框view背景色
    hud.bezelView.backgroundColor = [UIColor clearColor];
    
    // 1秒之后再消失
    [hud hideAnimated:YES afterDelay:0.5];
    
}

/**
 *  手动关闭MBProgressHUD
 */
+ (void)wh_hideHUD
{
    [self hideHUDForView:nil];
}

/**
 *  手动关闭MBProgressHUD
 *
 *  @param view    显示MBProgressHUD的视图
 */
+ (void)hideHUDForView:(UIView *)view
{
    if (view == nil) view = [UIApplication sharedApplication].delegate.window;
    [self hideHUDForView:view animated:YES];
}

@end
